<?php
namespace Beerus\Modules\DecoCompany\Controllers;

use Doraemon\model\DecoCompanyAuth;
use Doraemon\model\DecoCompanyEffect;
use Doraemon\tools\Hhzurl;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\service\User;

class EffectController
{

    public function index(Request $request, Content $content)
    {
        $pageSize = $request->input("per_page", 20);
        $page = $request->input("page", 1);
        $params = $request->input();

        $search = [];
        $search['effect_status'] = DecoCompanyEffect::STATUS_NORMAL;
        if (!empty($params['uid'])) {
            $search['uid'] = $params['uid'];
        }
        if (!empty($params['nick'])) {
            $uid = User::getUidByNick($params['nick']);
            $search['uid'] = $uid;
        }
        if (!empty($params['update_time']['start'])) {
            $search['update_time[>=]'] = strtotime($params['update_time']['start']. ' 00:00:00');
        }
        if (!empty($params['update_time']['end'])) {
            $search['update_time[<=]'] = strtotime($params['update_time']['end']. ' 23:59:59');
        }

        $map['AND'] = $search;
        $map['ORDER'] = ['update_time' => 'DESC'];
        $map['LIMIT'] = [
            ($page - 1) * $pageSize,
            $pageSize
        ];
        $list = DecoCompanyEffect::select('*', $map);
        $total = DecoCompanyEffect::count($search);
        foreach ($list as $key => $value) {
            $list[$key]['house_area'] = $value['house_area'] / 100;
            $list[$key]['quoted_price'] = $value['quoted_price'] / 100;
        }

        $gridModel = new pGridDataModel($list, $total, [
            'perPage' => $pageSize
        ]);
        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->disableCreateButton();

                $grid->filter(function($filter){
                    // 去掉默认的id过滤器
                    $filter->disableIdFilter();
                    $filter->column(1 / 3, function ($filter) {
                        $filter->equal('uid', '用户UID')->placeholder('请输入uid');
                    });
                    $filter->column(1 / 3, function ($filter) {
                        $filter->equal('nick', '用户昵称')->placeholder('请输入昵称');
                    });
                    $filter->column(1 / 2, function ($filter) {
                        $filter->between('update_time', '更新时间')->date('Y-m-d');
                    });
                    $filter->expand();
                });

                $grid->column('enterprise_name', '公司名称')->display(function () {
                    $info = DecoCompanyAuth::getByUid($this->uid);
                    return $info ? $info["enterprise_name"] : "";
                })->width(100);
                $grid->column("uid", '用户uid');
                $grid->column("case_name", '案例名称');
                $grid->column("quoted_price", '装修报价(万元)');
                $grid->column("house_area", '面积(㎡)');
                $grid->column("house_type", '户型')->display(function () {
                    return self::formatHouseType($this->house_type);
                });
                $grid->column("quoted_price_desc", '报价说明');
                $grid->column("update_time", '更新时间')->display(function () {
                    return $this->update_time ? date("Y-m-d H:i:s", $this->update_time) : '';
                });
                $grid->actions(function (Grid\Displayers\Actions $actions) {
                    $actions->disableDelete();
                    $actions->disableEdit();
                    $actions->disableView();
                    $link = Hhzurl::getDomain() . "/decoration-company/diagram-detail?isPreview=1&did={$actions->row->eid}";
                    $actions->prepend('<a class="fa" href="javascript:layer.open({
                                type: 2,
                                title: \'预览\',
                                maxmin: true,
                                area: [\'450px\', \'700px\'],
                                content: \'' . $link . '\'
                            });">预览</a>');
                });
            });

        $tab = new Tab();
        $tab->add('效果图列表', $gridList->render(), true);
        $content->row($tab);
        return $content
            ->header('效果图列表')
            ->description(' ')
            ->breadcrumb(['text' => '效果图列表']);
    }

    private static function formatHouseType($house_type) {
        $house_type = explode(',', $house_type);
        $type = ['室','厅','厨','卫','阳台'];
        $res = [];
        while ($house_type && $type) {
            $res[] = array_shift($house_type) . array_shift($type);
        }
        return implode('', $res);
    }

}